package com.hxkcloud.patient.aerotel.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.aerotel.domain.health.AerotelECGPdu;


/**
 * TODO: document your custom view class.
 */
public class ECGGridView extends View {

    private final static int COLUMNS = 50;
    private final static int ROWS = 40;

    private Paint paint;
    private Paint datapaint;

    private TextPaint mTextPaint;

    private Path path;

    private AerotelECGPdu ecg = null;

    public ECGGridView(Context context) {
        super(context);
        init(null, 0);
    }

    public ECGGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public ECGGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.ECGGridView, defStyle, 0);
        a.recycle();

//        setBackgroundColor(Color.WHITE);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(1);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.ROUND);

        datapaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        datapaint.setStyle(Paint.Style.STROKE);
        datapaint.setColor(Color.BLUE);
        datapaint.setStrokeWidth(3);
        datapaint.setStrokeCap(Paint.Cap.ROUND);
        datapaint.setStrokeJoin(Paint.Join.ROUND);

        // Set up a default TextPaint object
        mTextPaint = new TextPaint();
        mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextAlign(Paint.Align.LEFT);
        mTextPaint.setTextSize(48);
        mTextPaint.setColor(Color.BLACK);

        path = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(this.getLayoutParams().width, this.getLayoutParams().height);
    }

    private void drawBackground(Canvas canvas) {

        int width = this.getWidth();
        int height = this.getHeight();

        float gsize = width / 50f;

        canvas.drawRect(0, 0, width, height, paint);

        float x1 = 0, y1 = 0;
        float x2 = 0, y2 = height;
        for (int i = 0; i < 50; i++) {
            x1 = i * gsize;
            canvas.drawLine(x1, 0, x1, height, paint);
        }
        x1 = 0;
        y1 = 0;
        x2 = width;
        y2 = 0;
        for (int j = 0; j < 60; j++) {
            y2 = j * gsize;
            canvas.drawLine(0, y2, width, y2, paint);
        }

        float offsetTextWidth = gsize * 12.5f;
        float offsetTextHeight = gsize * 15f;
        float offsetx = gsize, offsety = gsize;
        canvas.drawText("I",
                offsetx,
                offsety,
                mTextPaint);
        offsety += offsetTextHeight;
        canvas.drawText("II",
                offsetx,
                offsety,
                mTextPaint);
        offsety += offsetTextHeight;
        canvas.drawText("III",
                offsetx,
                offsety,
                mTextPaint);
        offsety += offsetTextHeight;
        canvas.drawText("Rhythm",
                offsetx,
                offsety,
                mTextPaint);

        offsetx += offsetTextWidth;
        offsety = gsize;
        canvas.drawText("aVR",
                offsetx,
                offsety,
                mTextPaint);
        offsety += offsetTextHeight;
        canvas.drawText("aVL",
                offsetx,
                offsety,
                mTextPaint);
        offsety += offsetTextHeight;
        canvas.drawText("aVF",
                offsetx,
                offsety,
                mTextPaint);

        offsetx += offsetTextWidth;
        offsety = gsize;
        canvas.drawText("V1",
                offsetx,
                offsety,
                mTextPaint);
        offsety += offsetTextHeight;
        canvas.drawText("V2",
                offsetx,
                offsety,
                mTextPaint);
        offsety += offsetTextHeight;
        canvas.drawText("V3",
                offsetx,
                offsety,
                mTextPaint);

        offsetx += offsetTextWidth;
        offsety = gsize;
        canvas.drawText("V4",
                offsetx,
                offsety,
                mTextPaint);
        offsety += offsetTextHeight;
        canvas.drawText("V5",
                offsetx,
                offsety,
                mTextPaint);
        offsety += offsetTextHeight;
        canvas.drawText("V6",
                offsetx,
                offsety,
                mTextPaint);
    }

    private void drawData(Canvas canvas) {
        float bheight = getHeight() / 4;
        float bwidth = getWidth() / 4;
        float space = bwidth / 938;
        float offsety = 0, offsetx = 0;
        float zeroline = bheight / 2;
        // block I
        offsety += bheight;
        float x = 0;
        float y = zeroline;
        float zeroliney = 0;
        path.moveTo(x, y);
        byte[] datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_I);
        for (byte b : datum) {
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }
        datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_AVR);
        for (byte b : datum) {
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }
        datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_V1);
        for (byte b : datum) {
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }
        datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_V4);
        for (byte b : datum) {
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }

        zeroliney += bheight;
        x = 0;
        y = zeroline + bheight;
        path.moveTo(x, y);
        datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_II);
        for (int i = 0; i < 938; i++) {
            byte b = datum[i];
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }
        datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_AVL);
        for (byte b : datum) {
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }
        datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_V2);
        for (byte b : datum) {
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }
        datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_V4);
        for (byte b : datum) {
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }

        zeroliney += bheight;
        x = 0;
        y = zeroline + bheight * 2;
        path.moveTo(x, y);
        datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_III);
        for (byte b : datum) {
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }
        datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_AVF);
        for (byte b : datum) {
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }
        datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_V3);
        for (byte b : datum) {
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }
        datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_V6);
        for (byte b : datum) {
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }

        zeroliney += bheight;
        x = 0;
        y = zeroline + bheight * 3;
        path.moveTo(x, y);
        datum = this.ecg.data.getLeadData(AerotelECGPdu.LEAD_II);
        for (byte b : datum) {
            x += space;
            y = zeroliney + zeroline - bheight * (2f / 3f) * (b / 256f);
            path.lineTo(x, y);
        }
        canvas.drawPath(path, datapaint);
    }

    public void setEcg(AerotelECGPdu pdu) {
        this.ecg = pdu;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBackground(canvas);
        drawData(canvas);
    }
}
